<?php
/**
 * Created by PhpStorm.
 * User: dong
 * Date: 2018/3/1
 * Time: 19:19
 */
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class NinePayPayment extends RechargeBaseAbstract implements WAPable
{
    protected $gw_pay = 'http://www.939pay.com/Payapi_Index_Pay.html';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($data['P_ErrCode'] == 0) {
            if ($this->veryCallbackSign($data)) {
                return true;
            }
        }
        return false;

    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $signStr = $data['P_UserId'] . '|';
        $signStr .= $data['P_OrderId'] . '|';
        $signStr .= $data['P_CardId'] . '|';
        $signStr .= $data['P_CardPass'] . '|';
        $signStr .= $data['P_FaceValue'] . '|';
        $signStr .= $data['P_ChannelId'] . '|';
        $signStr .= $data['P_ErrCode'] . '|';
        $signStr .= $this->getMchKey();
        if ($data['P_PostKey'] == md5($signStr)) {
            return true;
        }
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['P_OrderId'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['P_FaceValue'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::NinePay;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::WX => 22,
            PaymentMap::WX_WAP => 20,
            PaymentMap::ALI => 2,
            PaymentMap::ALI_WAP => 1,
            PaymentMap::QQ => 21,
            PaymentMap::QQ_WAP => 12
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() . '?' . http_build_query($this->getParameters());
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $sign = $this->getParameter('P_UserId') . '|';
        $sign .= $this->getParameter('P_OrderId') . '|';
        $sign .= $this->getParameter('P_CardId') . '|';
        $sign .= $this->getParameter('P_CardPass') . '|';
        $sign .= $this->getParameter('P_FaceValue') . '|';
        $sign .= $this->getParameter('P_ChannelId') . '|';
        $sign .= $this->getMchKey();
        return md5($sign);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'errCode=0';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        $mchData = $rechargeOrder->order_data;
        // TODO: Implement initParameters() method.
        $this->setParameter('P_UserId', $this->getMchId());
        $this->setParameter('P_OrderId', $rechargeOrder->plat_no);
        $this->setParameter('P_CardId', '');
        $this->setParameter('P_CardPass', '');
        $this->setParameter('P_FaceValue', $rechargeOrder->order_amt);
        $this->setParameter('P_ChannelId', $this->getPaymentMap($mchData['recharge_type']));
        $this->setParameter('P_Subject', 'aiispay');
        $this->setParameter('P_Price', $rechargeOrder->order_amt);
        $this->setParameter('P_Quantity', 1);
        $this->setParameter('P_Description', '');
        $this->setParameter('P_Notic', $this->setRemark($rechargeOrder));
        $this->setParameter('P_Result_url', $this->getCallbackUrl());
        $this->setParameter('P_Notify_url', $this->getReturnUrl());
        $this->setParameter('P_PostKey', $this->paySign());
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        $url = $this->getGwPay() . '?' . http_build_query($this->getParameters());
        return new HrefRedirectAction($url);
    }
}
